<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Personal;
use App\Models\User;
use Illuminate\Http\Request;

class PersonalController extends AdminController
{

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model'] = 'personal';
        $this->_response['_active']['_action'] = '';
        $this->_response['_title'] = '小一农货-个人中心';
    }
    //个人中心首页
    public function index(Request $request){
        $search=$request->all();
        $page = $request->get('page', 1);
        $this->_response['_active']['_action']      = 'list';

        $PersonalModel = new Personal();
        $data = $PersonalModel->getData($search, $page, 20);

        $this->_response['personal'] = $data;

        $this->_response['personal']['pageData']['page'] = $page;
        $this->_response['personal']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['personal']['pageData']['lastPage'] , '/admin/personal?');
        //return  $this->_response['list'];

        return view('admin.personal.list', $this->_response);
    }
    //积分修改
    public function edit($id){
        $this->_response['_active']['_action']      = 'edit';
        $userModel = new User();
        $this->_response['personal'] = $userModel::where('id', $id)->first();
        //return $this->_response;
        return view('admin.personal.edit', $this->_response);
    }
    public function doEdit(Request $request){
        $data = $request->all();
        $saveEdit = [
            'id' => $data['id'],
            'integral' => $data['integral'],
            'updated_at' => date('Y-m-d H:i:s', time())
        ];
        $personalModel = new Personal();
        if($personalModel->saveEdit($saveEdit,$data['id'])){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }
    }
    //个人中心删除页面
    public function delete($id){
        $Del =[
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $PersonalModel = new Personal();
        if($PersonalModel->Del($Del,$id)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }
}